package com.net.shop.car.manager;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.google.gson.Gson;
import com.net.shop.car.manager.thirdparty.volley.BitmapCache;
import com.net.shop.car.manager.ui.user.LoginActivity;
import com.net.shop.car.manager.ui.view.TitleView;
import com.net.shop.car.manager.utils.AnimUtils;
import com.net.shop.car.manager.utils.BaseUtils;
import com.net.shop.car.manager.utils.NetworkUtils;
import com.net.shop.car.manager.utils.VolleyUtils;

/**
 * @description 所有活动应该继承到基类<br>
 *              提供了活动常用的对象和方法
 * @author wangpeng
 * @date 2014-12-20
 */
public class BaseActivity extends FragmentActivity implements Response.ErrorListener {
	/** 布局加载器 */
	public LayoutInflater inflater;
	/** 碎片管器器 */
	public FragmentManager fragmentManager;
	/** 网络请求队列 */
	public RequestQueue requestQueue;
	/** 图片加载器 */
	public ImageLoader imageLoader;
	/** 图片内存缓存容器 */
	public BitmapCache bitmapCache;
	/** Json格式字符串与对象装换工具 */
	public Gson gson;
	/** 网络请求工具 */
	public VolleyUtils volleyUtils;
	public TitleView titleView;
	/**
	 * 
	 */
	public ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ActivityCollector.addActivity(this);

		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		fragmentManager = getSupportFragmentManager();
		requestQueue = App.i().requestQueue;
		imageLoader = App.i().imageLoader;
		bitmapCache = App.i().bitmapCache;

		gson = new Gson();
		volleyUtils = VolleyUtils.init(requestQueue, this, this);
		titleView = (TitleView) findViewById(R.id.titleView);
		
		progressDialog = new ProgressDialog(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStop() {
		super.onStop();
		requestQueue.cancelAll(App.i());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityCollector.removeActivity(this);
	}

	/**
	 * 启动新活动
	 * 
	 * @param target
	 *            目标活动类名
	 * @param params
	 *            传递参数 <b>注：格式以key，value成双成对设置</b>
	 */
	public void actionStart(Class<?> target, Object... params) {
		actionStart(target, BaseUtils.initBundle(params));
	}

	/**
	 * 启动新活动
	 * 
	 * @param target
	 *            目标活动类名
	 * @param bundle
	 *            传递参数
	 */
	public void actionStart(Class<?> target, Bundle bundle) {
		Intent intent = new Intent(this, target);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
	}

	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);
		overridePendingTransition(R.anim.activity_right_enter, R.anim.activity_left_exit);
	}

	@Override
	public void finish() {
		super.finish();
		if (ActivityCollector.size() > 1) {
			overridePendingTransition(R.anim.activity_left_enter, R.anim.activity_right_exit);
		}
	}

	/** 保存Object到sp */
	public void saveToSharedPreferences(String key, Object value) {
		SharedPreferences sp = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
		sp.edit().putString(key, value.toString()).commit();
	}

	/** 从sp获取object */
	public String getFromSharedPreferences(String key) {
		SharedPreferences sp = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
		return sp.getString(key, null);
	}

	/** 保存String到sp */
	public void saveToSharedPreferences(String key, String value) {
		SharedPreferences sp = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
		sp.edit().putString(key, value).commit();
	}

	/** 从sp获取object */
	public String getStringFromSharedPreferences(String key) {
		SharedPreferences sp = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
		return sp.getString(key, null);
	}

	/** 保存boolean到sp */
	public void saveToSharedPreferences(String key, boolean value) {
		SharedPreferences sp = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
		sp.edit().putBoolean(key, value).commit();
	}

	/** 从sp获取object */
	public boolean getBooleanFromSharedPreferences(String key) {
		SharedPreferences sp = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
		return sp.getBoolean(key, false);
	}

	public void showToast(int textId) {
		showToast(getString(textId));
	}

	public void showToast(String text) {
		View view = inflater.inflate(R.layout.toast_message, null);
		((TextView) view.findViewById(R.id.tx_message)).setText(text);
		Toast mToast = new Toast(getApplicationContext());
		mToast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100);
		mToast.setDuration(500);
		mToast.setView(view);
		mToast.show();
	}

	public void startAnimation(View view, Animation animation) {
		view.setVisibility(View.VISIBLE);
		view.clearAnimation();
		view.startAnimation(animation);
	}

	public void endAnimation(View view, Animation animation) {
		view.clearAnimation();
		view.startAnimation(animation);
		view.setVisibility(View.GONE);
	}

	@Override
	public void onErrorResponse(VolleyError error) {
		showToast(NetworkUtils.isDisconnected(this) ? R.string.error_net : R.string.error_request);
	}

	public OnClickListener onBackClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			startAnimation(view, AnimUtils.init().addAlpha(1f, 0.2f, 0, 200, new LinearInterpolator(), Animation.REVERSE).create());
			finish();
		}
	};

	/**
	 * @param num String
	 */
	public void call(String num) {
		Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + num)); 	
	    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
	    startActivity(intent);
	}
	
	/**
	 * 
	 */
	public void gotoLogin() {
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
	}
}
